package com.heu.blood.storge.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heu.blood.dict.entity.DictTypeEntity;
import com.heu.blood.dict.vo.DictTypeVo;
import com.heu.blood.storge.entity.DictTemplateEntryEntity;
import com.heu.blood.storge.vo.DictTemplateQueryVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;

import com.heu.blood.storge.dao.DictTemplateDao;
import com.heu.blood.storge.entity.DictTemplateEntity;
import com.heu.blood.storge.service.DictTemplateService;


@Service("dictTemplateService")
public class DictTemplateServiceImpl extends ServiceImpl<DictTemplateDao, DictTemplateEntity> implements DictTemplateService {

    @Autowired
    private DictTemplateDao dictTemplateDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        DictTemplateEntity dictTemplateEntity = new DictTemplateEntity();

        if (Strings.isNotBlank((String) params.get("dictType"))) {
            dictTemplateEntity.setDictType((String) params.get("dictType"));
        }
        LambdaQueryWrapper<DictTemplateEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Strings.isNotBlank(dictTemplateEntity.getDictType()), DictTemplateEntity::getDictType, dictTemplateEntity.getDictType());
        IPage<DictTemplateEntity> page = this.page(
                new Query<DictTemplateEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<DictTemplateQueryVo> queryDictTemplateList() {

        LambdaQueryWrapper<DictTemplateEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<DictTemplateEntity> dictTemplateEntities = dictTemplateDao.selectList(queryWrapper);

        List<DictTemplateQueryVo> list = new ArrayList<>();
        for (DictTemplateEntity entity: dictTemplateEntities) {
            DictTemplateQueryVo dictTypeVo = new DictTemplateQueryVo();
            dictTypeVo.setId(entity.getId());
            dictTypeVo.setCode(entity.getDictType());
            dictTypeVo.setLabel(entity.getDictCode());
            list.add(dictTypeVo);
        }

        return list;
    }
}